package cn.echogo.framework.widget.imagezoom;

/**
 * https://gist.github.com/3781644
 * 
 * @version 1.0
 * @author Stan Rong 2012-10-11
 */
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class ImageViewTouchViewPager extends ViewPager {

	private static final String TAG = "ImageViewTouchViewPager";

	public ImageViewTouchViewPager(Context context) {
		super(context);
		init();
	}

	public ImageViewTouchViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	@Override
	protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
		if (v instanceof ImageViewTouch) {
			return ((ImageViewTouch) v).canScroll(dx);
		} else {
			return super.canScroll(v, checkV, dx, x, y);
		}
	}

	private void init() {
		setOnPageChangeListener(new SimpleOnPageChangeListener() {
			
			int positionSelected = 0;
			
//			@Override
//			public void onPageScrollStateChanged(int state) {
//				Log.d(TAG, "onPageScrollStateChanged :: " + state);
//				super.onPageScrollStateChanged(state);
//				try {
//					ImageViewTouch imageViewTouch = (ImageViewTouch) getChildAt(getCurrentItem());
//					if (imageViewTouch != null) {
//						imageViewTouch.zoomTo(1f, 300);
//					}
//				}
//				catch (ClassCastException ex) {
//					Log.e(TAG, "This view pager should have only ImageViewTouch as a children.", ex);
//				}
//			}
			
			@Override
			public void onPageSelected(int position) {
				super.onPageSelected(position);
				PhotoPagerAdapter adapter = (PhotoPagerAdapter) getAdapter();
				adapter.resetScale(positionSelected);
				positionSelected = position;
				
			}
			
		});
	}
}
